package com.lapck.car1;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
//import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

public class figura1 extends View {
	private float x;
	private float y;
	
	private float Ultimox;
	private float Ultimoy;
	
	private float centerx;
	private float centery;
	
	private final int radio;
	private final Paint myPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
	
	private boolean enfocado = false;
	private long tiempo;
	private float distanciatotal = 0;
	private float velocidad;
	
	private Pista vistapista;
	
	 private TextView mStatusText;
	
	public figura1(Context c, float x, float y, int r) {
		super(c);
		myPaint.setColor(0xFFFF0000);
		this.x= x;
		this.y = y;
		this.radio = r;
		this.centerx =x;
		this.centery =y;
		//this.setLayoutParams(new FrameLayout.LayoutParams(r*4, r*4));
	}
	
	@Override
	protected void onDraw(Canvas canvas){
		super.onDraw(canvas);
		
		canvas.save();
		canvas.drawCircle(x, y, radio, myPaint);
		canvas.translate(x, y);
		canvas.restore();
	}
	
	 public void setTextView(TextView textView) {
	        mStatusText = textView;
	 }
	 public void setPista(Pista pista){
		 vistapista = pista;
	 }
	@Override
	public boolean onTouchEvent(MotionEvent evento){
		final int accion = evento.getAction();


		if ((evento.getX() <= x + this.radio && evento.getY() <= y + this.radio && evento.getX() >= x - this.radio && evento.getY() >= y - this.radio)|| enfocado){
			//Log.v("mensaje", "getx="+evento.getX()+" gety="+evento.getY()+" Ultimox="+x+" Ultimoy="+y);
			switch (accion){
			case MotionEvent.ACTION_DOWN : {
				final float x2 = evento.getX();
				final float y2 = evento.getY();
				
				distanciatotal = 0;
				Ultimox = x2;
				Ultimoy = y2;
				enfocado = true;
				tiempo = System.currentTimeMillis();
			}
			case MotionEvent.ACTION_MOVE: {
				final float x2 = evento.getX();
				final float y2 = evento.getY();
				
				final float distanciax = x2 - Ultimox;
				final float distanciay = y2 - Ultimoy;
				
				x += distanciax;
				y += distanciay;
				
				distanciatotal += Math.sqrt((distanciax) * (distanciax) + (distanciay) * (distanciay));;
				
				Ultimox = x2;
				Ultimoy = y2;
				
				invalidate();
				break;
				
			}
			case MotionEvent.ACTION_UP: {
				

				long tiemp1;
				enfocado = false;
				tiemp1 = System.currentTimeMillis() - tiempo;
				velocidad = (distanciatotal / (tiemp1))*1000;
				mStatusText.setText(String.valueOf(velocidad));
				/* Log.v("mensaje", "Tiempo="+tiemp1+" Distancia="+distanciatotal+" Velocidad="+velocidad );*/
				//gotoCentro();
				//Log.v("mensaje", "X="+x +" CentroX="+centerx+" Y="+y+" CentroY="+centery );	
				//TranslateAnimation anim = new TranslateAnimation(Animation.RELATIVE_TO_PARENT , centerx - x, Animation.RELATIVE_TO_PARENT, centery - y);
				x =centerx;
				y=centery;
				TranslateAnimation anim = new TranslateAnimation(-(centerx - Ultimox), 0 , -(centery - Ultimoy), 0);
				anim.setDuration(500);
				//vistapista.muevepista(velocidad/100, velocidad/100);
				vistapista.muevepista((-(centerx - Ultimox))*(velocidad/1000), (-(centery - Ultimoy))*(velocidad/1000));
//				Log.v("mensaje", "X="+x +" CentroX="+centerx+" Y="+y+" CentroY="+centery );	
				Ultimoy = y;
				Ultimox = x;
				this.startAnimation(anim);

			}
		}
		}
		return true;
	}
}
